package com.wwz.swordfingeroffer;

import org.junit.Test;

/**
 * 百度教育一面
 */
public class swordFinger2 {
    @Test
    public void test() {
        System.out.println(addBinary("1010","1011"));
    }
    public String addBinary(String a, String b) {
        StringBuilder ans = new StringBuilder();
        int max = Math.max(a.length(), b.length());
        // 进位
        int carry = 0;
        for (int i = 0; i < max; i++) {
            carry += i < a.length() ? a.charAt(a.length() - i - 1) - '0' : 0;
            carry += i < b.length() ? b.charAt(a.length() - i - 1) - '0' : 0;
            // 取余 1+1 则需要进位 所以当前位为0
            ans.append((char)(carry % 2 + '0'));
            carry /=2;
        }

        if (carry > 0) {
            ans.append('1');
        }
        ans.reverse();
        return ans.toString();

    }
}
